package com.ruoyi.mushcoin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.mushcoin.domain.TgUserTaskRewards;
import com.ruoyi.mushcoin.domain.bo.TgUserTaskRewardsBo;
import com.ruoyi.mushcoin.domain.vo.TgUserTaskRewardsVo;
import com.ruoyi.mushcoin.mapper.TgUserTaskRewardsMapper;
import com.ruoyi.mushcoin.service.ITgUserTaskRewardsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static com.ruoyi.mushcoin.domain.table.TgUserTaskRewardsTableDef.TG_USER_TASK_REWARDS;

/**
 * 用户任务奖励列(H5)Service业务层处理
 *
 * @author billy
 * 2024-08-18
 */
@Service
public class TgUserTaskRewardsServiceImpl extends BaseServiceImpl<TgUserTaskRewardsMapper, TgUserTaskRewards> implements ITgUserTaskRewardsService {
    @Resource
    private TgUserTaskRewardsMapper tgUserTaskRewardsMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(TG_USER_TASK_REWARDS);
    }

    private QueryWrapper buildQueryWrapper(TgUserTaskRewardsBo tgUserTaskRewardsBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TG_USER_TASK_REWARDS.USER_ID.eq(tgUserTaskRewardsBo.getUserId()));
        queryWrapper.and(TG_USER_TASK_REWARDS.TASK_TYPE.eq(tgUserTaskRewardsBo.getTaskType()));
        queryWrapper.and(TG_USER_TASK_REWARDS.TASK_REWARDS.eq(tgUserTaskRewardsBo.getTaskRewards()));
        queryWrapper.and(TG_USER_TASK_REWARDS.TASK_DATE.eq(tgUserTaskRewardsBo.getTaskDate()));
        queryWrapper.and(TG_USER_TASK_REWARDS.RE_USER_ID.eq(tgUserTaskRewardsBo.getReUserId()));
        queryWrapper.and(TG_USER_TASK_REWARDS.TASK_USER_TAG.eq(tgUserTaskRewardsBo.getTaskUserTag()));

        return queryWrapper;
    }

    /**
     * 查询用户任务奖励列(H5)
     *
     * @param id 用户任务奖励列(H5)主键
     * @return 用户任务奖励列(H5)
     */
    @Override
    public TgUserTaskRewardsVo selectById(Long id) {
        return this.getOneAs(query().where(TG_USER_TASK_REWARDS.ID.eq(id)), TgUserTaskRewardsVo.class);

    }

    /**
     * 查询用户任务奖励列(H5)列表
     *
     * @param tgUserTaskRewardsBo 用户任务奖励列(H5)Bo
     * @return 用户任务奖励列(H5)集合
     */
    @Override
    public List<TgUserTaskRewardsVo> selectList(TgUserTaskRewardsBo tgUserTaskRewardsBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgUserTaskRewardsBo);
        return this.listAs(queryWrapper, TgUserTaskRewardsVo.class);
    }

    /**
     * 分页查询用户任务奖励列(H5)列表
     *
     * @param tgUserTaskRewardsBo 用户任务奖励列(H5)Bo
     * @return 分页用户任务奖励列(H5)集合
     */
    @Override
    public TableDataInfo<TgUserTaskRewardsVo> selectPage(TgUserTaskRewardsBo tgUserTaskRewardsBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgUserTaskRewardsBo);
        queryWrapper.select("t1.*", "concat(user.first_name,' ',user.last_name) realName");
        queryWrapper.from(TgUserTaskRewards.class).as("t1");
        queryWrapper.leftJoin(new QueryTable("tg_user_info").as("user")).on("user.user_id=t1.user_id");
        Page<TgUserTaskRewardsVo> page = this.pageAs(PageQuery.build(), queryWrapper, TgUserTaskRewardsVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增用户任务奖励列(H5)
     *
     * @param tgUserTaskRewardsBo 用户任务奖励列(H5)Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TgUserTaskRewardsBo tgUserTaskRewardsBo) {
        TgUserTaskRewards tgUserTaskRewards = MapstructUtils.convert(tgUserTaskRewardsBo, TgUserTaskRewards.class);


        return this.save(tgUserTaskRewards);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增用户任务奖励列(H5)，前台提供主键值，一般用于导入的场合
     *
     * @param tgUserTaskRewardsBo 用户任务奖励列(H5)Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TgUserTaskRewardsBo tgUserTaskRewardsBo) {
        TgUserTaskRewards tgUserTaskRewards = MapstructUtils.convert(tgUserTaskRewardsBo, TgUserTaskRewards.class);


        return tgUserTaskRewardsMapper.insertWithPk(tgUserTaskRewards) > 0;//前台传来主键值
    }

    /**
     * 修改用户任务奖励列(H5)
     *
     * @param tgUserTaskRewardsBo 用户任务奖励列(H5)Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TgUserTaskRewardsBo tgUserTaskRewardsBo) {
        TgUserTaskRewards tgUserTaskRewards = MapstructUtils.convert(tgUserTaskRewardsBo, TgUserTaskRewards.class);
        if (ObjectUtil.isNotNull(tgUserTaskRewards) && ObjectUtil.isNotNull(tgUserTaskRewards.getId())) {
            boolean updated = this.updateById(tgUserTaskRewards);
            return updated;
        }
        return false;
    }


    /**
     * 批量删除用户任务奖励列(H5)
     *
     * @param ids 需要删除的用户任务奖励列(H5)主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
